package com.sntk.organauthomanager.domain.model;

import com.sntk.organauthomanager.infrastructure.exception.DataNoLegalException;
import com.sntk.organauthomanager.infrastructure.exception.ObjectDisbaledException;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * @ClassName: Organization
 * @author: wgchao
 * @createTime: 2020/7/17 3:05 PM
 * @Description: 机构
 */
@Getter
public class Organization extends AggregateRoot {

    //机构编号-机构编号规则（子机构编号=父机构编号+4位随机码）
    private String institutionNumber;

    //机构名称
    private String institutionName;

    //机构地址
    private String institutionalAddress;

    //备注
    private String remark;

    //对用HIS机构编号
    private String hisInstitutionNumber;

    //上级机构编号
    private String superiorNumber;

    private boolean disabled;


    public Organization(Long id, String institutionNumber, String institutionName, String institutionalAddress,
                        String hisInstitutionNumber, String remark, String superiorNumber, boolean disabled, Long version) {
        if(StringUtils.isEmpty(institutionNumber)) {
            throw new DataNoLegalException("机构编号不能为空");
        }
        if(StringUtils.isEmpty(institutionName)) {
            throw new DataNoLegalException("机构名称不能为空");
        }
        setId(id);
        this.institutionNumber = institutionNumber;
        this.institutionName = institutionName;
        this.institutionalAddress = institutionalAddress;
        this.hisInstitutionNumber = hisInstitutionNumber;
        this.remark = remark;
        this.superiorNumber = superiorNumber;
        this.disabled = disabled;
        setVersion(version);
    }

    public static Organization generate(String institutionNumber, String institutionName, String institutionalAddress,
                                        String hisInstitutionNumber, String remark, Organization superiorOgi){
        if(superiorOgi != null){
            superiorOgi.normalException();
        }
        return new Organization(-1L, institutionNumber, institutionName, institutionalAddress, hisInstitutionNumber, remark,
                superiorOgi == null ? null:superiorOgi.institutionNumber, false, null);
    }

    public void modifyOrganInfo(String institutionName, String institutionalAddress, String hisInstitutionNumber,
                                String remark, boolean disabled){

        if(StringUtils.isEmpty(institutionName)) {
            throw new DataNoLegalException("机构名称不能为空");
        }
        this.institutionName = institutionName;
        this.institutionalAddress = institutionalAddress;
        this.hisInstitutionNumber = hisInstitutionNumber;
        this.remark = remark;
        this.disabled = disabled;

    }

    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!normal()){
            throw new ObjectDisbaledException(this.institutionName+"机构不可用");
        }
    }

    /**
     *  添加用户
     * @return
     */
    public User addUser(String uname, String password, String userName, String hisCode, Long hisDepartmentCode, String shorthandCode,
                        String phoneNumber, String remark, Integer userType){
        normalException();
        return User.generate(uname, this, password, userName, hisCode, hisDepartmentCode, shorthandCode, phoneNumber, remark, userType);
    }

    /**
     * 添加角色
     * @param roleCode
     * @param roleName
     * @param remark
     * @return
     */
    public Role addRole(String roleCode, String roleName, String remark){
        normalException();
        return Role.generate(this, roleCode, roleName, remark);
    }

    /**
     * 判断是否未父子机构
     * @param institutionNumber
     * @return
     */
    public boolean fatherOrSonOrgan(String institutionNumber){
        if(institutionNumber.equals("3702") ||
                this.institutionNumber.equals("3702")){
            return false;
        }
        if(this.institutionNumber.startsWith(institutionNumber) ||
                institutionNumber.startsWith(this.institutionNumber)){
            return true;
        }
        return false;
    }

}
